<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>TypeScript知识点总结 | MyBlog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/markdown-blog/favicon.ico">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/markdown-blog/assets/css/0.styles.25640b18.css" as="style"><link rel="preload" href="/markdown-blog/assets/js/app.4ad08097.js" as="script"><link rel="preload" href="/markdown-blog/assets/js/3.ddd67056.js" as="script"><link rel="preload" href="/markdown-blog/assets/js/1.7a25059f.js" as="script"><link rel="preload" href="/markdown-blog/assets/js/30.6b0a16e0.js" as="script"><link rel="prefetch" href="/markdown-blog/assets/js/10.f00be53f.js"><link rel="prefetch" href="/markdown-blog/assets/js/11.7ac2e99b.js"><link rel="prefetch" href="/markdown-blog/assets/js/12.f11c009c.js"><link rel="prefetch" href="/markdown-blog/assets/js/13.be750ad0.js"><link rel="prefetch" href="/markdown-blog/assets/js/14.09d576e3.js"><link rel="prefetch" href="/markdown-blog/assets/js/15.b4c1727e.js"><link rel="prefetch" href="/markdown-blog/assets/js/16.4890e6f7.js"><link rel="prefetch" href="/markdown-blog/assets/js/17.feac5f26.js"><link rel="prefetch" href="/markdown-blog/assets/js/18.b49467b1.js"><link rel="prefetch" href="/markdown-blog/assets/js/19.29b4407f.js"><link rel="prefetch" href="/markdown-blog/assets/js/20.9ee55973.js"><link rel="prefetch" href="/markdown-blog/assets/js/21.b6da110b.js"><link rel="prefetch" href="/markdown-blog/assets/js/22.c64d89ab.js"><link rel="prefetch" href="/markdown-blog/assets/js/23.1249a0d2.js"><link rel="prefetch" href="/markdown-blog/assets/js/24.858426bc.js"><link rel="prefetch" href="/markdown-blog/assets/js/25.1eaccce9.js"><link rel="prefetch" href="/markdown-blog/assets/js/26.6d8a66e7.js"><link rel="prefetch" href="/markdown-blog/assets/js/27.bef6e2f6.js"><link rel="prefetch" href="/markdown-blog/assets/js/28.1d169f61.js"><link rel="prefetch" href="/markdown-blog/assets/js/29.f6bd6a28.js"><link rel="prefetch" href="/markdown-blog/assets/js/31.3a6f1d19.js"><link rel="prefetch" href="/markdown-blog/assets/js/4.46f777c5.js"><link rel="prefetch" href="/markdown-blog/assets/js/5.db5e4405.js"><link rel="prefetch" href="/markdown-blog/assets/js/6.8b37b343.js"><link rel="prefetch" href="/markdown-blog/assets/js/7.cacb4248.js"><link rel="prefetch" href="/markdown-blog/assets/js/8.6174f008.js"><link rel="prefetch" href="/markdown-blog/assets/js/9.4f9a61ea.js">
    <link rel="stylesheet" href="/markdown-blog/assets/css/0.styles.25640b18.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>MyBlog</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2></p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>kasnars</span>
            
          <span data-v-25ba6db2>2021 - </span>
          2024
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/markdown-blog/" class="home-link router-link-active"><img src="/markdown-blog/wsq.webp" alt="MyBlog" class="logo"> <span class="site-name">MyBlog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/markdown-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><a href="/markdown-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/markdown-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/markdown-blog/docs/theme-reco/" class="nav-link router-link-active"><i class="undefined"></i>
  前端知识点
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/docs/computer/" class="nav-link"><i class="undefined"></i>
  计算机基础
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/docs/others/" class="nav-link"><i class="undefined"></i>
  其他技术栈
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/个人作品/" class="nav-link"><i class="undefined"></i>
  个人作品
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/解决方案/" class="nav-link"><i class="undefined"></i>
  解决方案
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/BUG记录/" class="nav-link"><i class="undefined"></i>
  BUG记录
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/技术整理/" class="nav-link"><i class="undefined"></i>
  技术整理
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/前端总结/" class="nav-link"><i class="undefined"></i>
  前端总结
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      链接
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/kasnars" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://juejin.cn/user/2287450072292567" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  稀土掘金
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/markdown-blog/wsqgif.gif" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    kasnars
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>21</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>14</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/markdown-blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><a href="/markdown-blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/markdown-blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/markdown-blog/docs/theme-reco/" class="nav-link router-link-active"><i class="undefined"></i>
  前端知识点
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/docs/computer/" class="nav-link"><i class="undefined"></i>
  计算机基础
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/docs/others/" class="nav-link"><i class="undefined"></i>
  其他技术栈
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/个人作品/" class="nav-link"><i class="undefined"></i>
  个人作品
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/解决方案/" class="nav-link"><i class="undefined"></i>
  解决方案
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/BUG记录/" class="nav-link"><i class="undefined"></i>
  BUG记录
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/技术整理/" class="nav-link"><i class="undefined"></i>
  技术整理
</a></li><li class="dropdown-item"><!----> <a href="/markdown-blog/categories/前端总结/" class="nav-link"><i class="undefined"></i>
  前端总结
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      链接
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/kasnars" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://juejin.cn/user/2287450072292567" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-juejin"></i>
  稀土掘金
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><a href="/markdown-blog/docs/theme-reco/htmlcss.html" class="sidebar-link">HTML+CSS知识点总结</a></li><li><a href="/markdown-blog/docs/theme-reco/js.html" class="sidebar-link">JavaScript知识点总结</a></li><li><a href="/markdown-blog/docs/theme-reco/ts.html" aria-current="page" class="active sidebar-link">TypeScript知识点总结</a></li><li><a href="/markdown-blog/docs/theme-reco/vue.html" class="sidebar-link">Vue知识点总结</a></li><li><a href="/markdown-blog/docs/theme-reco/react.html" class="sidebar-link">React知识点总结</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>TypeScript知识点总结</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>kasnars</span>
            
          <span data-v-25ba6db2>2021 - </span>
          2024
        </a></span></div></div> <div data-v-1aefc0b4><main class="page" style="padding-right:0;"><section><div class="page-title"><h1 class="title">TypeScript知识点总结</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>kasnars</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2/8/2022</span></i> <i class="iconfont reco-eye" data-v-f875f3fc><span id="/markdown-blog/docs/theme-reco/ts.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-f875f3fc><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>TypeScript</span><span class="tag-item" data-v-f875f3fc>面经</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="typescript知识点总结"><a href="#typescript知识点总结" class="header-anchor">#</a> TypeScript知识点总结</h1> <h2 id="ts基础概念"><a href="#ts基础概念" class="header-anchor">#</a> TS基础概念</h2> <h3 id="ts的优缺点-为什么要用ts"><a href="#ts的优缺点-为什么要用ts" class="header-anchor">#</a> TS的优缺点，为什么要用TS</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>代码的可读性和可维护性(类型提醒)</li> <li>编译阶段就能发现很多错误，避免线上bug</li> <li>增强了编辑器和IDE的功能，方便代码补全，接口提示等</li></ul> <hr> <ul><li>学习成本增加，接口，泛型等前端原本不常用的概念</li> <li>前期会增加一些开发成本</li> <li>一些js库和框架需要对ts做兼容，例如vue2对ts的支持就不好</li> <li>ts的编译需要时间，项目大后打包编译时间耗费比js大</li></ul></details> <h2 id="ts语法"><a href="#ts语法" class="header-anchor">#</a> TS语法</h2> <h3 id="ts里常见的类型"><a href="#ts里常见的类型" class="header-anchor">#</a> TS里常见的类型</h3> <details class="custom-block details"><summary>点击查看</summary> <p>常用：</p> <ul><li>boolean, number, string, array, enum, any, void<br>
不常用：</li> <li>tuple null undefined never</li></ul></details> <h3 id="interface和type的区别"><a href="#interface和type的区别" class="header-anchor">#</a> interface和type的区别</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>interface可以进行类型继承，type不行</li></ul> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token keyword">interface</span> <span class="token class-name">Hero</span> <span class="token punctuation">{</span>
  name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
  age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
  skill<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
  skinNum<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
  <span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span> <span class="token comment">// say函数返回值为string</span>
  <span class="token punctuation">[</span>propname<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">]</span><span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">;</span> <span class="token comment">// 当前Hero可定义任意字符串类型的key</span>
<span class="token punctuation">}</span>
<span class="token comment">// 继承</span>
<span class="token keyword">interface</span> <span class="token class-name">littleSoldier</span> <span class="token keyword">extends</span> <span class="token class-name">Hero</span> <span class="token punctuation">{</span>
  <span class="token function">rush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 任意类型</span>
<span class="token keyword">interface</span> <span class="token class-name">IAnyObject</span> <span class="token punctuation">{</span>
  <span class="token punctuation">[</span>key<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">]</span><span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">type</span> <span class="token class-name">Hero</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
  name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span>
  age<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span>
  skill<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span>
  skinNum<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div></details> <h3 id="数组类型及对象数组类型声明"><a href="#数组类型及对象数组类型声明" class="header-anchor">#</a> 数组类型及对象数组类型声明</h3> <details class="custom-block details"><summary>点击查看代码</summary> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token keyword">interface</span> <span class="token class-name">IItem</span> <span class="token punctuation">{</span>
  id<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
  name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
  isGod<span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 对象数组</span>
<span class="token keyword">const</span> objectArr<span class="token operator">:</span> IItem<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> id<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'俊劫'</span><span class="token punctuation">,</span> isGod<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// or</span>
<span class="token keyword">const</span> objectArr<span class="token operator">:</span> <span class="token builtin">Array</span><span class="token operator">&lt;</span>IItem<span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> id<span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'俊劫'</span><span class="token punctuation">,</span> isGod<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">// 普通数组</span>
<span class="token keyword">const</span> numberArr<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> arr<span class="token operator">:</span> <span class="token punctuation">(</span><span class="token builtin">number</span> <span class="token operator">|</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;string&quot;</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></details> <h3 id="tuple和array的区别"><a href="#tuple和array的区别" class="header-anchor">#</a> tuple和array的区别</h3> <details class="custom-block details"><summary>点击查看代码</summary> <p>array仅需要类型对应，而tuple需要对应位置(下标)的类型以及数量全部对应才行</p> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token comment">// 数组 某个位置的值可以是注解中的任何一个</span>
<span class="token keyword">const</span> <span class="token constant">LOL</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token builtin">string</span> <span class="token operator">|</span> <span class="token builtin">number</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;zed&quot;</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token string">&quot;darts&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token comment">// 元祖 每一项数据类型必须一致</span>
<span class="token keyword">const</span> <span class="token constant">LOL</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;zed&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;darts&quot;</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></details> <h3 id="联合类型-和交叉类型"><a href="#联合类型-和交叉类型" class="header-anchor">#</a> 联合类型|和交叉类型&amp;</h3> <details class="custom-block details"><summary>点击查看代码</summary> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token comment">// anjiao 某胖博主爱好</span>
<span class="token keyword">interface</span> <span class="token class-name">Waiter</span> <span class="token punctuation">{</span>
  anjiao<span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
  <span class="token function-variable function">say</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">interface</span> <span class="token class-name">Teacher</span> <span class="token punctuation">{</span>
  anjiao<span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
  <span class="token function-variable function">skill</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 联合类型</span>
<span class="token keyword">function</span> <span class="token function">judgeWho</span><span class="token punctuation">(</span>animal<span class="token operator">:</span> Waiter <span class="token operator">|</span> Teacher<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token comment">// 交叉类型 </span>
<span class="token comment">// 同名类型会进行合并，同名基础类型属性的合并返回：never</span>
<span class="token comment">// 同名非基础类型属性可以正常合并</span>
<span class="token keyword">function</span> <span class="token function">judgeWho</span><span class="token punctuation">(</span>jishi<span class="token operator">:</span> Waiter <span class="token operator">&amp;</span> Teacher<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></details> <h3 id="泛型-是什么"><a href="#泛型-是什么" class="header-anchor">#</a> 泛型&lt;&gt;是什么</h3> <details class="custom-block details"><summary>点击查看代码</summary> <ul><li>泛型&lt;&gt;表示一个暂不确定的类型，具体的类型在调用的时候传入，T仅仅是一个占位符</li></ul> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token comment">// T 自定义名称</span>
<span class="token keyword">function</span> <span class="token generic-function"><span class="token function">myFun</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token constant">T</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>params<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> params<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token generic-function"><span class="token function">myFun</span> <span class="token generic class-name"><span class="token operator">&lt;</span><span class="token builtin">string</span><span class="token operator">&gt;</span></span></span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">&quot;123&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;456&quot;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 定义多个泛型</span>
<span class="token keyword">function</span> <span class="token generic-function"><span class="token function">join</span><span class="token generic class-name"><span class="token operator">&lt;</span><span class="token constant">T</span><span class="token punctuation">,</span> <span class="token constant">P</span><span class="token operator">&gt;</span></span></span><span class="token punctuation">(</span>first<span class="token operator">:</span> <span class="token constant">T</span><span class="token punctuation">,</span> second<span class="token operator">:</span> <span class="token constant">P</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>first<span class="token interpolation-punctuation punctuation">}</span></span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>second<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token generic-function"><span class="token function">join</span> <span class="token generic class-name"><span class="token operator">&lt;</span><span class="token builtin">number</span><span class="token punctuation">,</span> <span class="token builtin">string</span><span class="token operator">&gt;</span></span></span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></details> <h3 id="断言是什么"><a href="#断言是什么" class="header-anchor">#</a> 断言是什么</h3> <details class="custom-block details"><summary>点击查看代码</summary> <ul><li>as或者&lt;&gt;表示断言，指定当前值的类型</li> <li>jsx中必须用as</li></ul> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token keyword">function</span> <span class="token function">judgeWho</span><span class="token punctuation">(</span>animal<span class="token operator">:</span> Waiter <span class="token operator">|</span> Teacher<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>animal<span class="token punctuation">.</span>anjiao<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token punctuation">(</span>animal <span class="token keyword">as</span> Teacher<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">skill</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
    <span class="token punctuation">(</span>animal <span class="token keyword">as</span> Waiter<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">say</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></details> <h3 id="never和void的区别"><a href="#never和void的区别" class="header-anchor">#</a> never和void的区别</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>void表示没有任何类型，never表示永远不存在的值的类型</li></ul></details> <h3 id="ts类里有哪些关键字"><a href="#ts类里有哪些关键字" class="header-anchor">#</a> TS类里有哪些关键字</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>public</li> <li>private 类的外部不可用，继承也不行</li> <li>protected 类的外部不可用，继承可以</li> <li>public readOnly xxx 只读属性</li> <li>static funcXXX 静态方法，不需要 new 就可以调用</li> <li>abstract funcXXX 抽象类，所有子类都必须要实现 funcXXX</li></ul></details> <h2 id="ts常见工具类型"><a href="#ts常见工具类型" class="header-anchor">#</a> TS常见工具类型</h2> <h3 id="readonly"><a href="#readonly" class="header-anchor">#</a> Readonly&lt;&gt;</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>将T中所有类型设为只读</li></ul> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token keyword">type</span> <span class="token class-name">JUserModel3</span> <span class="token operator">=</span> Readonly<span class="token operator">&lt;</span>UserModel<span class="token operator">&gt;</span>

<span class="token comment">// =</span>
<span class="token keyword">type</span> <span class="token class-name">JUserModel3</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token keyword">readonly</span> name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
    <span class="token keyword">readonly</span> age<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">|</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span>
    <span class="token keyword">readonly</span> sex<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></details> <h3 id="pick"><a href="#pick" class="header-anchor">#</a> Pick&lt;&gt;</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>在一个声明好的对象中，挑选一部分出来组成一个新的声明对象</li></ul> <div class="language-ts line-numbers-mode"><pre class="language-ts"><code><span class="token keyword">interface</span> <span class="token class-name">Todo</span> <span class="token punctuation">{</span>
  title<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
  description<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
  done<span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">type</span> <span class="token class-name">TodoBase</span> <span class="token operator">=</span> Pick<span class="token operator">&lt;</span>Todo<span class="token punctuation">,</span> <span class="token string">&quot;title&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;done&quot;</span><span class="token operator">&gt;</span><span class="token punctuation">;</span>

<span class="token comment">// =</span>
<span class="token keyword">type</span> <span class="token class-name">TodoBase</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    title<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">;</span>
    done<span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></details> <h3 id="nonnullable"><a href="#nonnullable" class="header-anchor">#</a> NonNullable&lt;&gt;</h3> <details class="custom-block details"><summary>点击查看</summary> <ul><li>去掉T中的所有null和undefined类型</li></ul></details> <p><em>该页面数据由本人编写+网上论坛整理所得</em></p> <p><em>参考数据地址</em></p> <ul><li><a href="https://juejin.cn/post/6981728323051192357" target="_blank" rel="noopener noreferrer">一篇够用的TypeScript总结<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2/22/2022, 11:29:59 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/markdown-blog/docs/theme-reco/js.html" class="prev">
            JavaScript知识点总结
          </a></span> <span class="next"><a href="/markdown-blog/docs/theme-reco/vue.html">
            Vue知识点总结
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:0;" data-v-cb1513f6></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/markdown-blog/assets/js/app.4ad08097.js" defer></script><script src="/markdown-blog/assets/js/3.ddd67056.js" defer></script><script src="/markdown-blog/assets/js/1.7a25059f.js" defer></script><script src="/markdown-blog/assets/js/30.6b0a16e0.js" defer></script>
  </body>
</html>
